<?php


class GoodsnormalApp extends BackendApp
{
	var $_goodsnormal_mod;

	function __construct()
	{
		$this->GoodsnormalApp();
	}

	function GoodsnormalApp()
	{
		parent::BackendApp();
		$this->_goodsnormal_mod =& m('goodsnormal');
	}

	function index()
	{
		$conditions .= "1=1 ";
		if($_GET['g_code']!=''){
			$conditions.=" and g_code like '%".$_GET['g_code']."%'";
		}
		if($_GET['g_name']!=''){
			$conditions.=" and g_name like '%".$_GET['g_name']."%'";
		}
		if($_GET['g_owner']!=''){
			$conditions.=" and g_owner like '%".$_GET['g_owner']."%'";
		}
		$page   =   $this->_get_page(10);   //获取分页信息

		$sql="SELECT n.*,m.user_name ,IFNULL(x.ws_tid,0) as ws_tid FROM zys_goods_normal n
		LEFT JOIN zys_member m ON n.g_optid=m.user_id
		LEFT JOIN (SELECT ws_tid,IFNULL(ws_gid,0) as ws_gid from zys_waybill_sub_normal GROUP BY ws_gid) x on n.g_tid=ws_gid
		 where 
		 $conditions ORDER BY g_opdate DESC limit $page[limit]";


		 $goodsnormal=$this->_goodsnormal_mod->getAll($sql);

		 $sql="select count(1) from zys_goods_normal where $conditions";
		 $page['item_count']=$this->_goodsnormal_mod->getOne($sql);   //获取统计数据


		 $this->_format_page($page);
		 $this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		 $this->assign('auth', $goodsnormal);
		 $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
		 $this->assign('tabself', json_encode(array('text'=>'货单管理')));
		 $this->display('goodsnormal.index.html');
	}



	//新增
	function add(){
		if (!IS_POST){
			$goodsnormal=array();
			$goodsnormal['g_code']=gmtime().rand(10000,99999);
			$goodsnormal['user_name']=$this->visitor->get('user_name');
			$this->assign('auth', $goodsnormal);
			$this->assign('tabself', json_encode(array('text'=>'新建货单')));
			$this->assign('tabback', json_encode(array('text'=>'货单管理','url'=>'')));
			$this->display('goodsnormal.form.html');
		}else{

			$data = array(
                'g_code'  => $_POST['g_code'],
                'g_name'  => $_POST['g_name'],
                'g_piece' => $_POST['g_piece'],
                'g_weight'	     => $_POST['g_weight'],
			    'g_size'      => $_POST['g_size'],

				'g_piece_residue' => $_POST['g_piece'],
                'g_weight_residue'	     => $_POST['g_weight'],
			    'g_size_residue'      => $_POST['g_size'],

			    'g_owner'	     => $_POST['g_owner'],
			    'g_owner_tel'      => $_POST['g_owner_tel'],
				'g_loading_place'      => $_POST['g_loading_place'],
			    'g_unloading_place'	     => $_POST['g_unloading_place'],
			    'g_consignee'      => $_POST['g_consignee'],
			    'g_consignee_tel'	     =>$_POST['g_consignee_tel'],
			    'g_consignor'	     => $_POST['g_consignor'],
			    'g_consignor_tel'      => $_POST['g_consignor_tel'],
			    'g_note'	     => $_POST['g_note'],
			    'g_optid'      => $this->visitor->get('user_id'),
			    'g_opdate'	     => gmtime()
			);

			/* 保存 */
			$rows = $this->_goodsnormal_mod->add($data);
			if ($this->_goodsnormal_mod->has_error())
			{
				$this->show_warning($this->_goodsnormal_mod->get_error());
				return;
			}

			$this->show_message('添加成功',
                '返回货单',   'index.php?app=goodsnormal&act=edit&id=' . $rows
                );
		}
	}




	//编辑
	function edit()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

		if (!IS_POST){
			$conditions=" 1 =1 and g_tid=".$id;
			$sql="SELECT n.*,m.user_name FROM zys_goods_normal n LEFT JOIN zys_member m ON n.g_optid=m.user_id where $conditions  limit 1";
			$goodsnormal=$this->_goodsnormal_mod->getRow($sql);

			$this->assign('id', $id);


			$this->assign('auth', $goodsnormal);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->assign('tabself', json_encode(array('text'=>'货单编辑')));
			$this->assign('tabback', json_encode(array('text'=>'货单管理','url'=>'')));
			$this->display('goodsnormal.form.html');
		}else
		{
			$data = array(
                'g_name'  => $_POST['g_name'],
                'g_piece' => $_POST['g_piece'],
                'g_weight'	     => $_POST['g_weight'],
			    'g_size'      => $_POST['g_size'],
			    'g_owner'	     => $_POST['g_owner'],
			    'g_owner_tel'      => $_POST['g_owner_tel'],
				'g_loading_place'      => $_POST['g_loading_place'],
			    'g_unloading_place'	     => $_POST['g_unloading_place'],
			    'g_consignee'      => $_POST['g_consignee'],
			    'g_consignee_tel'	     =>$_POST['g_consignee_tel'],
			    'g_consignor'	     => $_POST['g_consignor'],
			
				'g_piece_residue' => $_POST['g_piece'],
                'g_weight_residue'	     => $_POST['g_weight'],
			    'g_size_residue'      => $_POST['g_size'],
			
			    'g_consignor_tel'      => $_POST['g_consignor_tel'],
			    'g_note'	     => $_POST['g_note']
			);

			/* 保存 */
			$rows = $this->_goodsnormal_mod->edit($id, $data);
			if ($this->_goodsnormal_mod->has_error())
			{
				$this->show_warning($this->_goodsnormal_mod->get_error());
				return;
			}

			$this->show_message('编辑成功',
                '继续编辑',   'index.php?app=goodsnormal&act=edit&id=' . $id
			);
		}

	}

	function view(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!IS_POST){
			$conditions=" 1 =1 and g_tid=".$id;
			$sql="SELECT n.*,m.user_name FROM zys_goods_normal n LEFT JOIN zys_member m ON n.g_optid=m.user_id where $conditions  limit 1";
			$goodsnormal=$this->_goodsnormal_mod->getRow($sql);

			$this->assign('id', $id);
			$this->assign('auth', $goodsnormal);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->assign('tabself', json_encode(array('text'=>'货单详情')));
			$this->assign('tabback', json_encode(array('text'=>'货单管理','url'=>'')));
			$this->display('goodsnormal.view.html');
		}
	}

	//删除
	function drop(){
		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';
		$db=&db();
		$arrs=explode(',', $arr);
		if($arrs!=null&&count($arrs)>0){
			foreach ($arrs as $v){

				$sql="SELECT n.*,m.user_name ,IFNULL(x.ws_tid,0) as ws_tid FROM zys_goods_normal n
		LEFT JOIN zys_member m ON n.g_optid=m.user_id
		LEFT JOIN (SELECT ws_tid,IFNULL(ws_gid,0) as ws_gid from zys_waybill_sub_normal GROUP BY ws_gid) x on n.g_tid=ws_gid WHERE g_tid=".$v;
				$status=$db->getRow($sql);
				if($status&&$status['ws_tid']){

				}else{
					if (!$this->_goodsnormal_mod->drop($v)){
						$this->show_warning($this->_goodsnormal_mod->get_error());
						return;
					}
				}
			}
		}else{
			$sql="SELECT n.*,m.user_name ,IFNULL(x.ws_tid,0) as ws_tid FROM zys_goods_normal n
		LEFT JOIN zys_member m ON n.g_optid=m.user_id
		LEFT JOIN (SELECT ws_tid,IFNULL(ws_gid,0) as ws_gid from zys_waybill_sub_normal GROUP BY ws_gid) x on n.g_tid=ws_gid WHERE g_tid=".$arr;
			$status=$db->getRow($sql);
			if($status&&$status['ws_tid']){
				$this->show_message('此货单不能删除');
				return ;
			}else{
				if (!$this->_goodsnormal_mod->drop($arr)){
					$this->show_warning($this->_goodsnormal_mod->get_error());
					return;
				}
			}
		}
		$this->show_message('删除成功');
			
	}
}